package com.ict.txmsv2.controller.system;

import com.ict.txmsv2.model.Equipment;
import com.ict.txmsv2.model.Lend;
import com.ict.txmsv2.model.MethodDescrition;
import com.ict.txmsv2.model.RespBean;
import com.ict.txmsv2.services.EquipService;
import com.ict.txmsv2.services.LendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author linzzz
 * @desc ...
 * @date 2020-12-19
 */
@RestController
@RequestMapping("/system/equip")
public class SystemEquipController {

    @Autowired
    EquipService equipService;

    @Autowired
    LendService lendService;

    @GetMapping("/")
    @MethodDescrition("查看设备")
    public List<Equipment> getAllEquip() {
        return equipService.getAllEquip();
    }

    @MethodDescrition("删除设备")
    @DeleteMapping("/{id}")
    public RespBean delEquip(@PathVariable Integer id) {
        if (equipService.delEquip(id) == 1) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("失败");
    }

    @PutMapping("/")
    @MethodDescrition("修改设备信息")
    public RespBean updateEquip(@RequestBody Equipment equipment) {
        if (equipService.updateEquip(equipment) == 1) {
            return RespBean.ok("修改成功");
        }
        return RespBean.error("失败");
    }

    @PostMapping("/")
    @MethodDescrition("批量添加设备")
    public RespBean addEquips(Equipment equipment, MultipartFile file, int count) throws IOException {
        if (equipService.addEquips(equipment, file, count) == count) {
            return RespBean.ok("添加成功");
        } else return RespBean.error("失败了");
    }

    @DeleteMapping("/many/{ids}")
    @MethodDescrition("批量删除")
    public RespBean delMany(@PathVariable Integer[] ids) {
        if (equipService.delMany(ids) == ids.length) {
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @GetMapping("/lend")
    @MethodDescrition("设备租借情况")
    public List<Lend> getAllLend(String lName, String lTime, Integer status, String returnTime) {
        return lendService.getAllLend(lName, lTime, status, returnTime);
    }

    @PostMapping("/lend")
    @MethodDescrition("添加借记录")
    public RespBean addLend(@RequestBody Lend lend){
        if (lendService.addLend(lend) == 1){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("失败了");
    }

    @PutMapping("/lend")
    @MethodDescrition("修改出借设备状况")
    public RespBean updateLend(@RequestBody Lend lend){
        if (lendService.updateLend(lend) == 1){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("失败");
    }
}
